package com.cmict.data.bpm.framework.flowable.core.listener;

import com.cmict.data.bpm.dal.dataobject.task.BpmProcessInstanceExtDO;
import com.cmict.data.bpm.service.listener.event.ReportEvent;
import com.cmict.data.bpm.service.message.UserMessageListService;
import com.cmict.data.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import com.cmict.data.bpm.service.task.BpmProcessInstanceService;
import com.google.common.collect.ImmutableSet;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 监听 {@link ProcessInstance} 的开始与完成，创建与更新对应的 {@link BpmProcessInstanceExtDO} 记录
 *
 * @author jason
 */
@Component
public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEventListener {

    @Resource
    @Lazy
    private BpmProcessInstanceService processInstanceService;

    @Resource
    private UserMessageListService messageListService;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
            .add(FlowableEngineEventType.PROCESS_CREATED)
            .add(FlowableEngineEventType.PROCESS_CANCELLED)
            .add(FlowableEngineEventType.PROCESS_COMPLETED)
            .build();

    public BpmProcessInstanceEventListener(){
        super(PROCESS_INSTANCE_EVENTS);
    }

    /**
     * 任务 新建  触发监听器
     * @param event
     */
    @Override
    protected void processCreated(FlowableEngineEntityEvent event) {
        processInstanceService.createProcessInstanceExt((ProcessInstance)event.getEntity());
    }

    /**
     *  流程未通过提醒
     * @param event
     */
    @Override
    protected void processCancelled(FlowableCancelledEvent event) {
        processInstanceService.updateProcessInstanceExtCancel(event);

        String processInstanceId = event.getProcessInstanceId();
        ProcessInstance processInstance = processInstanceService.getProcessInstance(processInstanceId);
        BpmMessageSendWhenTaskCreatedReqDTO reqDTO = new BpmMessageSendWhenTaskCreatedReqDTO();
        BeanUtils.copyProperties(processInstance, reqDTO);
        reqDTO.setStartUserId(Long.parseLong(processInstance.getStartUserId()));
        reqDTO.setProcessInstanceName(processInstance.getProcessDefinitionName());
        messageListService.sendMessageWhenProcessCancel(reqDTO);
    }

    /**
     *  流程通过提醒
     * @param event
     */
    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        processInstanceService.updateProcessInstanceExtComplete((ProcessInstance)event.getEntity());
        String processInstanceId = event.getProcessInstanceId();
        ProcessInstance processInstance = processInstanceService.getProcessInstance(processInstanceId);
        BpmMessageSendWhenTaskCreatedReqDTO reqDTO = new BpmMessageSendWhenTaskCreatedReqDTO();
        BeanUtils.copyProperties(processInstance, reqDTO);
        reqDTO.setStartUserId(Long.parseLong(processInstance.getStartUserId()));
        reqDTO.setProcessInstanceName(processInstance.getProcessDefinitionName());
        messageListService.sendMessageWhenProcessCompleted(reqDTO);
        String processDefinitionId = event.getProcessDefinitionId();
        applicationEventPublisher.publishEvent(new ReportEvent(processInstanceId, processDefinitionId));
    }
}
